package top.jlzhou.jobs.service.impl;

import top.jlzhou.jobs.entity.Outgoing;
import top.jlzhou.jobs.entity.UserInfo;
import top.jlzhou.jobs.mapper.OutgoingMapper;
import top.jlzhou.jobs.service.OutgoingService;
import top.jlzhou.jobs.service.UserInfoService;
import top.jlzhou.jobs.vo.OutgoingPage;
import top.jlzhou.jobs.vo.OutgoingVO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author J.L.Zhou
 * @since 2018-10-22
 */
@Service
public class OutgoingServiceImpl extends ServiceImpl<OutgoingMapper, Outgoing> implements OutgoingService {

	
	@Autowired
	private UserInfoService service;

	@Override
	public void add(Long token, Outgoing outgoing) throws Exception {
		UserInfo user = service.getByToken(token);
		outgoing.setUserName(user.getUserName());
		outgoing.setOutgoingEnd(LocalDateTime.now());
		baseMapper.insert(outgoing);
	}

	@Override
	public IPage<Outgoing> page(OutgoingPage page) {
		UserInfo user = service.getByToken(page.getToken());
		QueryWrapper<Outgoing> q = new QueryWrapper<Outgoing>()
		.orderByDesc("OUTGOING_START","OUTGOING_ID")
		.eq("USER_NAME", user.getUserName())
		;
		
		return baseMapper.selectPage(page, q);
	}

	@Override
	public IPage<OutgoingVO> pageVO(OutgoingPage page) {
		return baseMapper.page(page);
	}

	@Override
	public void delete(Long token, Long id) throws Exception {
		UserInfo user = service.getByToken(token);
		baseMapper.deleteById(id);
		
	}
	
}
